package cn.hl.hm.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

//import com.sun.media.jfxmedia.logging.Logger;

import cn.hl.hm.entity.FeedBack;
import cn.hl.hm.entity.FeedBackReply;
import cn.hl.hm.service.FeedbackService;
import cn.hl.hm.util.AppPager;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

/**
 * 
 * @author 大和 
 *
 */
@Controller
@RequestMapping("/tFeedbackController")
public class FeedbackController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	FeedbackService feedbackService;

	/**
	  * @Title: feedback  
	  * @Description: 提交反馈
	  * @date: 2017年1月5日
	  * @author: 大和
	  * @param userId
	  * @param appId
	  * @param apiVersion
	  * @param feedbackContent
	  * @param feedbackType
	  * @param request
	  * @param response
	  * @return String 
	  * @throws
	 */
	@RequestMapping(value = "/feedback", method = RequestMethod.POST)
	public @ResponseBody String feedback(
			@RequestParam("userId") Integer userId, // userId
			@RequestParam("type") Integer type, //1:发货端 2:司机端
			@RequestParam("appId") String appId, // 应用ID
			@RequestParam("apiVersion") String apiVersion, // api版本号
			@RequestParam("feedbackContent") String feedbackContent, // 内容
			@RequestParam("feedbackType") Integer feedbackType, // 反馈来源类型 网站 1
																// 、iOS2 App 3
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("个人中心---提交反馈---feedbackController/feedback");
		try {
			FeedBack feedback = new FeedBack();
			feedback.setUserId(userId);
			feedback.setAppId(appId);
			feedback.setContent(feedbackContent);
			feedback.setCreateTime(new Date());
			feedback.setFeedbackType(feedbackType);
			feedback.setVersion(apiVersion);
			feedback.setType(type);
			int add = feedbackService.add(feedback);
			if (add > 0) { 
				Result.ok(feedback, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
	/**
	  * @Title: feedbackList  
	  * @Description: 反馈列表
	  * @date: 2017年1月5日
	  * @author: 大和
	  * @param userId
	  * @param pageId
	  * @param pageCount
	  * @param request
	  * @param response
	  * @return String 
	  * @throws
	 */
	@RequestMapping(value = "/feedbackList", method = RequestMethod.POST)
	public @ResponseBody String feedbackList(
			@RequestParam("userId") Integer userId, // userId
			@RequestParam("pageId") Integer pageId, 
			@RequestParam("type") Integer type, 
			@RequestParam("pageCount") Integer pageCount,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("个人中心---获取意见反馈列表---feedbackController/feedbackList");
		try {
			AppPager handle = AppPager.handle(pageId, pageCount);
			FeedBack param = new FeedBack();
			param.setPageId(handle.getPageId());
			param.setPageCount(handle.getPageCount());
			param.setUserId(userId);
			param.setType(type);
			List<FeedBack> selectFeedBack = feedbackService.selectFeedBack(param);
			if(selectFeedBack!=null&&selectFeedBack.size()>0){
				for (FeedBack feedBack : selectFeedBack) {
					FeedBackReply re = new FeedBackReply();
					re.setFeedbackId(feedBack.getId());
					List<FeedBackReply> selectFeedBackReply = feedbackService.selectFeedBackReply(re);
					feedBack.setBackReplies(selectFeedBackReply);
				}
				
				Result.ok(selectFeedBack, InfoCode.SUCCESS, response);
				return null;
			}else {
				Result.ok(null ,InfoCode.SUCCESS, response);
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
	/**
	 * 
	  * @Title: reply  
	  * @Description: 反馈回复
	  * @date: 2017年1月5日
	  * @author: 大和
	  * @param feedId
	  * @param replyContent
	  * @param operId
	  * @param request
	  * @param response
	  * @return String 
	  * @throws
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.POST)
	public @ResponseBody String reply(
			@RequestParam("feedId") Integer feedId, // userId
			@RequestParam("replyContent") String replyContent, 
			@RequestParam("operId") Integer operId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("个人中心---回复反馈（运营后台用）---feedbackController/reply");
		try {
			
			FeedBackReply param = new FeedBackReply();
			param.setFeedbackId(feedId);
			param.setReplyContent(replyContent);
			param.setCreateTime(new Date());
		    int addReply = feedbackService.addReply(param);
		    if (addReply > 0) {
				Result.ok(param, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
	
	
	/**
	  * @Title: feedbackId  
	  * @Description: 查询某一条
	  * @date: 2017年1月5日
	  * @author: 大和
	  * @param feedbackId
	  * @param request
	  * @param response
	  * @return String 
	  * @throws
	 */
	@RequestMapping(value = "/feedbackInfo", method = RequestMethod.POST)
	public @ResponseBody String feedbackId(
			@RequestParam("feedbackId") Integer feedbackId, // userId
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("个人中心---查看反馈详情（包括回复）---feedbackController/feedbackInfo");
		try {
			FeedBack param = new FeedBack();
			param.setId(feedbackId);
			List<FeedBack> selectFeedBack = feedbackService.selectFeedBack(param);
			if(selectFeedBack!=null&&selectFeedBack.size()>0){
				FeedBack feedBack = selectFeedBack.get(0);
				FeedBackReply re = new FeedBackReply();
				re.setFeedbackId(feedBack.getId());
				List<FeedBackReply> selectFeedBackReply = feedbackService.selectFeedBackReply(re);
				feedBack.setBackReplies(selectFeedBackReply);
				Result.ok(feedBack, InfoCode.SUCCESS, response);
				return null;
			}else {
				Result.ok(null ,InfoCode.FAILURE, response);
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
}
